package com.study.wanandroid.module.login

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.ToastUtils
import com.study.wanandroid.R
import com.study.wanandroid.api.WanAndroidApi
import com.study.wanandroid.databinding.ActivityUserLoginBinding
import com.study.wanandroid.entity.LoginResponse
import com.study.wanandroid.net.ApiClient
import com.study.wanandroid.net.SimpleObserver
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * <pre>
 *     author : slx
 *     time   : 2020/11/28
 *     desc   :
 * </pre>
 */
class LoginActivity : AppCompatActivity() {

    lateinit var binding: ActivityUserLoginBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityUserLoginBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.userLoginBt.setOnClickListener(onClickListener)
    }

    private val onClickListener = View.OnClickListener {
        when (it.id) {
            R.id.user_login_bt -> {
                val username: String = binding.userPhoneEt.text.toString()
                val password: String = binding.userPasswordEt.text.toString()

                ApiClient.instance.instanceRetrofit(WanAndroidApi::class.java)
                        .login(username, password)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(object : SimpleObserver<LoginResponse>(this) {
                            override fun onSuccess(data: LoginResponse?) {
                                ToastUtils.showLong(data.toString())
                            }

                            override fun onFailure(errorMsg: String?) {
                                ToastUtils.showLong(errorMsg)
                            }

                        })
            }
        }
    }
}